package com.mgee.netty.server.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.protobuf.Any;
import com.mgee.netty.server.constant.MessagingConst;
import com.mgee.netty.server.netty.IoSession;
import com.mgee.netty.server.netty.SessionManager;
import com.mgee.netty.server.pojo.ChatUserInfo;
import com.mgee.netty.server.pojo.LobbyRoomInfo;
import com.mgee.netty.server.protobuf.ImProto.ChatRequest;
import com.mgee.netty.server.protobuf.ImProto.ChatRsp;
import com.mgee.netty.server.protobuf.ImProto.ImMsg;
import com.mgee.netty.server.util.MessageBuilder;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author mileswan
 *
 */
@Service
@Slf4j
public class ChatService {
	
	@Autowired
	private SessionManager sessionManager;
	
	
	public void sendMessage(ImMsg imMsg, IoSession currSession) {
		
		try {
			ChatUserInfo userInfo = currSession.getUser();
			if(userInfo == null) {
				currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "用户未登录!"));
				return;
			}
			String sender_uid = userInfo.getUid();
			ChatRequest chatRequest = imMsg.getMsg().unpack(ChatRequest.class);
			
			//检查是否在大厅房间内
			LobbyRoomInfo roomInfo = SessionManager.userId2LobbyRooms.get(sender_uid);
			//检查是否漫游房间内
			String roomId = SessionManager.userId2GroupIds.get(sender_uid);
			
			if(roomInfo == null && StringUtils.isBlank(roomId)) {
				currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "用户未在任何房间."));
				return;
			}
			
			if( StringUtils.isBlank(chatRequest.getToUsername()) ) { //发送房间内其他所有玩家
				if(roomInfo != null) { //发送大厅房间其他所有玩家
					sessionManager.sendMessageToLobbyRoomUsers(imMsg, sender_uid);
				}else {
					sessionManager.sendMessageToRoomUsers(imMsg, sender_uid);
				}
			}else { //发送指定玩家
				String toUsername = chatRequest.getToUsername();
				if(roomInfo != null) { //检查是否在同一个房间
					if(!roomInfo.equals(SessionManager.userId2LobbyRooms.get(toUsername))) {
						currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "目标用户不在同一个房间."));
						return;
					}
				}else {
					if(!roomId.equals(SessionManager.userId2GroupIds.get(toUsername))) {
						currSession.sendMessage(MessageBuilder.buildErrorResponse(imMsg, MessagingConst.RESPONSE_ERR_CODE_400, "目标用户不在同一个房间."));
						return;
					}
				}
				sessionManager.sendMessageTo(imMsg, toUsername);
			}
			
			//返回Chat Response消息
			ChatRsp.Builder chatRspBuilder = ChatRsp.newBuilder();
    		ImMsg.Builder imMsgBuilder = ImMsg.newBuilder();
    		chatRspBuilder.setResult(true);
    		chatRspBuilder.setDescription("已发送成功");
            
            imMsgBuilder.setMsg(Any.pack(chatRspBuilder.build()));
            imMsgBuilder.setId(imMsg.getId());
            
            //发送反馈消息
	        currSession.sendMessage(imMsgBuilder.build());
	        
		}catch(Exception e) {
			log.error("处理ChatRequest时发生异常, ", e);
		}
		
	}
	
}
